package cn.amaake.common.config;

import com.jfinal.handler.Handler;
import com.jfinal.kit.StrKit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BasePathHandler extends Handler {

    private String contextPathName;

    public BasePathHandler() {
        contextPathName = "CONTEXT_PATH";
    }

    public BasePathHandler(String contextPathName) {
        if (StrKit.isBlank(contextPathName)) {
            throw new IllegalArgumentException("contextPathName can not be blank.");
        }
        this.contextPathName = contextPathName;
    }

    public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
        String url = request.getScheme()+"://" + request.getServerName() //服务器地址
                + ":"
                + request.getServerPort()           //端口号
                + request.getContextPath() ;     //项目名称
        request.setAttribute(contextPathName, url);
        next.handle(target, request, response, isHandled);
    }

}
